package com.shimao.celnet.payment.controller.api;

import com.shimao.celnet.payment.form.PPayForm;
import com.shimao.celnet.payment.form.PPayInfoForm;
import com.shimao.celnet.payment.service.BEstateService;
import com.shimao.celnet.payment.service.PPayService;
import com.shimao.celnet.payment.service.SCodeNameService;
import com.shimao.celnet.payment.service.report.PPayDayReportService;
import com.shimao.celnet.payment.util.FinalUtil;
import com.shimao.celnet.payment.util.MyDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import java.util.ArrayList;
import java.util.List;

/**外部系统----》支付系统
 * Created by glean on 2018/9/13.
 * 数据：通过支付房间查询支付记录
 */
@RestController
@RequestMapping(value = "/payment")
@Api("接口：外部系统 → 支付系统，数据：获取分页支付记录")
public class CallIn_outPayRecordController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PPayService localService;

    @Autowired
    private SCodeNameService sCodeNameService;

    /**
     * 分页列表查询
     * @param formBean
     * @param
     * @return
     */
    @ApiOperation("外部获取支付记录请求")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PPayForm getOutPayRecordAll(@RequestBody @Valid PPayForm formBean,BindingResult result){

        logger.info("查询列表页面--开始");
        if(StringUtils.isNotEmpty(formBean.getGuid())){
            //更新，新增后，重新刷新情况，查询条件清空
            formBean=new PPayForm();
        }
        //当前页码设定
        int pageNum=FinalUtil.PAGE_Fist_NUM;

        if(formBean.isIsLastPage()){
            pageNum=formBean.getPages();
        }

        if(!StringUtils.isEmpty(String.valueOf(formBean.getPageNum())) && !"0".equals(String.valueOf(formBean.getPageNum()))){
            pageNum=formBean.getPageNum();
        }
        //查询开始日期
        if(StringUtils.isEmpty(formBean.getPayStartTime())) {
            formBean.setPayStartTime(MyDateUtil.StrToDateYmd(formBean.getPayStartTime()));
        }
        //查询结束日期
        if(StringUtils.isEmpty(formBean.getPayEndTime())) {
            formBean.setPayEndTime(MyDateUtil.StrToDateYmd(formBean.getPayEndTime()));
        }
        //页面数据查询结果
        if(formBean.getPageSize() == 0 ){
            formBean = localService.getByPage1(formBean, pageNum, FinalUtil.PAGE_LENGHT);
        }else {
            formBean = localService.getByPage1(formBean, pageNum, formBean.getPageSize());
        }
        List <PPayInfoForm>  infoForms = new ArrayList<PPayInfoForm>();
        List <PPayInfoForm>  infoForms1 = formBean.getList();
        if(infoForms1 != null && infoForms1.size()>0){
            for ( PPayInfoForm infoForm:
            infoForms1) {
                infoForm.setBusinessTypeName(sCodeNameService.getNameByCode(infoForm.getBusinessType()));
                if (FinalUtil.ALIPAY_PAY_TYPE.equals(infoForm.getPayType())){
                    infoForm.setPayTypeName("支付宝");
                }else if(FinalUtil.CUP_PAY_TYPE.equals(infoForm.getPayType())){
                    infoForm.setPayTypeName("银联");
                }else if(FinalUtil.WEChAT_PAY_TYPE.equals(infoForm.getPayType())){
                    infoForm.setPayTypeName("微信");
                }if("A04".equals(infoForm.getPayType())){
                    infoForm.setPayTypeName("停车推送");
                }
                if ("0".equals(infoForm.getPayStatus())){
                    infoForm.setPayStatusName("支付成功");
                }else if("1".equals(infoForm.getPayStatus())){
                    infoForm.setPayStatusName("支付失败");
                }else  if("2".equals(infoForm.getPayStatus())){
                    infoForm.setPayStatusName("交易处理中");
                }else if(infoForm.getPayStatus() == null && infoForm.getPayType() == null){
                    infoForm.setPayStatusName("未支付");
                }else if (infoForm.getPayStatus() == null && infoForm.getPayType() != null){
                    infoForm.setPayStatusName("支付失败");
                }
                infoForms.add(infoForm);
            }
        }
        logger.info("查询列表页面--结束");
        formBean.setList(infoForms);
        return formBean;
    }
}
